/**
 * @file 为Cocos增加的扩展功能就放在这里
 * @author DuskyHuang 圣子
*/

import { Color, GeometryRenderer, Mat4, Vec3, game } from "cc";
import { __private } from 'cc';
const col = new Color;
const points: Vec3[] = [];

/**
 * @description Cocos的GeometryRenderer不够用来扩展个曲线轨迹绘制吧
*/
Object.defineProperty(GeometryRenderer.prototype, 'addTrackCurve', {
    configurable: true, enumerable: false,
    value: function (center: Vec3, radius: number, color: Color, segments = 32, depthTest = true, useTransform = false, transform = new Mat4(),
        startArc: num.deg, anticlockwise: boolean = false, arcPercent: num.pct = 1) {
        points.length = 0;
        col.set(color);
        const start = Math.parseRadian(startArc) - Math.PI / 2;
        const { x: cx, y: cy, z: cz } = center;
        // let detPhi = Math.PI * 2.0 / segments * arcPercent, num = Math.round(segments * arcPercent);
        let detPhi = Math.PI * 2.0 / segments * arcPercent;
        if (!anticlockwise) detPhi = -detPhi;
        for (let i = 0; i < segments + 1; i++) {
            const rad = i * detPhi - start;
            const [x, z] = [radius * Math.cos(rad), radius * Math.sin(rad)];
            const p = new Vec3(cx + x, cy, cz + z);
            useTransform && Vec3.transformMat4(p, p, transform);
            points.push(p);
        }
        const dec = col.a / segments;
        for (let i = 0; i < segments; i++) {
            col.a -= dec;
            this.addLine(points[i], points[i + 1], col, depthTest);
        }
    },
});

const v3a = new Vec3;
const v3b = new Vec3;
const seg = new Vec3;

/**
 * @description Cocos的GeometryRenderer不够用来扩展个渐隐直线绘制吧
*/
Object.defineProperty(GeometryRenderer.prototype, 'addFadeLine', {
    configurable: true, enumerable: false,
    value: function (from: Vec3, to: Vec3, color: Color, segments = 32, depthTest = true) {
        points.length = 0;
        col.set(color);
        seg.set(to).subtract(from).multiplyScalar(1 / segments);
        v3a.set(from);
        v3b.set(from).add(seg);
        const dec = col.a / segments;
        for (let i = 0; i < segments; i++) {
            this.addLine(v3a, v3b, col, depthTest);
            v3a.add(seg);
            v3b.add(seg);
            col.a -= dec;
        }
    },
});

/**
 * @description Cocos的GeometryRenderer不够用来扩展个流动虚线绘制吧
*/
Object.defineProperty(GeometryRenderer.prototype, 'addFloatingDashLine', {
    configurable: true, enumerable: false,
    value: function (from: Vec3, to: Vec3, color: Color, segments = 8, depthTest = true,
        pct: num.ratio = 0.75, speed: num.ratio = 1) {
        const frac = timeFrac(speed);
        col.set(color);
        seg.set(to).subtract(from).multiplyScalar(1 / segments);
        this.addLine(v3a.set(from), Vec3.scaleAndAdd(v3b, from, seg, frac * pct), col, depthTest);
        Vec3.scaleAndAdd(v3a, from, seg, frac);
        Vec3.scaleAndAdd(v3b, v3a, seg, pct);
        for (let i = 0; i < segments; i++) {
            if (i === (segments - 1) && (frac > (1 - pct))) {
                this.addLine(v3a, to, col, depthTest);
            } else this.addLine(v3a, v3b, col, depthTest);
            Vec3.add(v3a, v3a, seg);
            Vec3.add(v3b, v3b, seg);
        }
    },
});

function timeFrac(speed: num.ratio): time.sec {
    const sec = game.totalTime * speed / 1000;
    return sec - Math.floor(sec);
}